<template>
  <div class="app-container">
    <Ht-search-form
      v-show="showSearch"
      :dictMap="dictMapAsync"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :advOptions="[]"
    ></Ht-search-form>

    <!--    <div class="page-label-block">-->
    <!--      <span class="color-block">|</span>-->
    <!--      <span class="page-label-name">-->
    <!--      租户管理-->
    <!--      </span>-->
    <!--    </div>-->

    <ht-table
      ref="dataTable"
      :multi-select="false"
      :showSerialNumber="false"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
      :opButtonList="opButtonList"
      :reqConfig="regConfig"
    >
      <template v-slot:operate>
        <el-table-column label="操作" align="left" width="50" fixed="right">
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: '明细',
                  doFunc: () => {
                    handleAction('detail', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>

    <!-- 添加或修改弹出框 -->
    <el-dialog :title="title"  center :visible.sync="open" width="1024px" append-to-body :close-on-click-modal="false">
      <el-form ref="detailForm" :model="detailForm" :rules="rules" label-width="120px">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="交易流水号" prop="id">
              <el-input v-model="detailForm.id" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同号码" prop="contractNo">
              <el-input v-model="detailForm.contractNo" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="合同类型" prop="contractType">
              <el-select style="width: 100%" v-model="detailForm.contractType" placeholder="合同类型" :disabled="true">
                <el-option
                  v-for="dict in dictMap.CONTRACT_TYPE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="付款人开户行行号" prop="payerAccBrno">
              <el-input v-model="detailForm.payerAccBrno" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款人开户行行名" prop="payerAccBrname">
              <el-input v-model="detailForm.payerAccBrname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="付款人账号" prop="payerActno">
              <el-input v-model="detailForm.payerActno" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="付款人户名" prop="payerName">
              <el-input v-model="detailForm.payerName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="收款人开户行行号" prop="payeeAccBrno">
              <el-input v-model="detailForm.payeeAccBrno" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="收款人开户行行名" prop="payeeAccBrname">
              <el-input v-model="detailForm.payeeAccBrname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="收款人账号" prop="payeeActno">
              <el-input v-model="detailForm.payeeActno" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="收款人户名" prop="payeeName">
              <el-input v-model="detailForm.payeeName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="交易币种" prop="curcd">
              <el-select style="width: 100%" v-model="detailForm.curcd" placeholder="交易币种" :disabled="true">
                <el-option
                  v-for="dict in dictMap.CURCD"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="交易金额上限" prop="amount">
              <ht-amount-input v-model="detailForm.amount" placeholder="交易金额上限" :disable="true"></ht-amount-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="生效日期" prop="effDate">
              <el-date-picker
                v-model="detailForm.effDate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="失效日期" prop="invDate">
              <el-date-picker
                v-model="detailForm.invDate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="登记日期" prop="openDate">
              <el-date-picker
                v-model="detailForm.openDate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="合同状态" prop="contractStatus">
              <el-select
                style="width: 100%"
                v-model="detailForm.contractStatus"
                placeholder="合同状态"
                :disabled="true"
              >
                <el-option
                  v-for="dict in dictMap.CONTRACT_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="24">
            <el-form-item label="附言" prop="remakrs">
              <el-input type="textarea" v-model="detailForm.remakrs" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button
          type="primary"
          @click="effective"
          v-loading.fullscreen.lock="loading"
          v-if="this.detailForm.contractStatus !== '01' && this.detailForm.contractStatus !== '02'"
        >
          生 效
        </el-button>
        <el-button
          type="primary"
          @click="start"
          v-loading.fullscreen.lock="loading"
          v-if="this.detailForm.contractStatus !== '00' && this.detailForm.contractStatus !== '01'"
        >
          开 启
        </el-button>
        <el-button
          type="primary"
          @click="pause"
          v-loading.fullscreen.lock="loading"
          v-if="this.detailForm.contractStatus !== '00' && this.detailForm.contractStatus !== '02'"
        >
          停 用
        </el-button>
        <el-button
          type="primary"
          @click="cancellation"
          v-loading.fullscreen.lock="loading"
          v-if="this.detailForm.contractStatus !== '00' && this.detailForm.contractStatus !== '02'"
        >
          注 销
        </el-button>
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPageMixin } from "@/libs/listPageMixin";
import {
  apiUrlConfigMaintenanceQuery,
  regularDebitContractMaintenanceCancel,
  regularDebitContractMaintenanceDetail,
  regularDebitContractMaintenanceEffective,
  regularDebitContractMaintenancePause,
  regularDebitContractMaintenanceStart,
} from "@/api/cnaps/beps/regularDebitContract";
import { amountThousandsFormatter } from "@/utils/table_col_formatter";
import HtAmountInput from "@/views/components/HtAmountInput";

export default {
  name: "RegularDebitContractMaintenance",
  mixins: [listPageMixin],
  components: { HtAmountInput },
  data() {
    return {
      // 查询参数
      queryParams: {
        contractNo: undefined,
        effDateMin: undefined,
        effDateMax: undefined,
        contractType: undefined,
      },
      loading: false,
      // 搜索
      formOptions: [],
      //
      searchFormDictList: ["CONTRACT_TYPE", "CONTRACT_STATUS", "CURCD"],
      // 列信息
      regConfig: apiUrlConfigMaintenanceQuery,
      tableBindColumns: [
        { prop: "id", label: "交易流水号", width: 180 },
        { prop: "contractType", label: "合同类型", dictCode: "CONTRACT_TYPE", width: 120 },
        { prop: "contractNo", label: "合同号码", width: 120 },
        { prop: "payerBrno", label: "付款人开户行行号", width: 180 },
        { prop: "payeeBrno", label: "收款人开户行行号", width: 180 },
        { prop: "effDate", label: "生效日期", type: "date", width: 120 },
        { prop: "invDate", label: "失效日期", type: "date", width: 120 },
        { prop: "amount", label: "交易金额上限", type: "price" },
      ],
      dictMapAsync: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      detailForm: {
        id: undefined,
        pkgId: undefined,
      },
      totalRecord: 0,
      totalPage: 0,
      needCount: 0,

      rules: {},
    };
  },
  mounted() {
    const formOptions = [
      // 普通搜索

      {
        type: "input",
        prop: "contractNo",
        defaultAttr: {
          size: "small",
          label: "合同号码",
          placeholder: "请输入合同号码",
        },
      },
      {
        type: "dates",
        nameObj: {
          startTimeName: "effDateMin",
          endTimeName: "effDateMax",
        },
        defaultAttr: {
          size: "small",
          label: "生效日期",
          placeholder: ["请输入起始生效日期", "请输入截止生效日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyyMMdd",
        },
      },

      {
        type: "dict",
        dict: "CONTRACT_TYPE",
        prop: "contractType",
        defaultAttr: {
          size: "small",
          label: "合同类型",
          placeholder: "请输入合同类型",
        },
      },
    ];
    this.formOptions = formOptions;
    this.dictMapAsync = this.getDictMap();
  },
  methods: {
    /***
     * 按钮操作汇总
     */
    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "detail":
          this.handleDetail(row);
          break;
      }
    },

    // 关闭按钮
    cancel() {
      this.open = false;
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.title = "定期借记合同维护审核明细";
      regularDebitContractMaintenanceDetail(row).then((response) => {
        this.detailForm = response.result;
        this.open = true;
      });
    },
    //点击生效按钮
    effective: function () {
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          regularDebitContractMaintenanceEffective(this.detailForm)
            .then((response) => {
              this.loading = true;
              this.msgSuccess("生效成功");
              this.open = false;
              this.loading = false;
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },

    //点击开启按钮
    start() {
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          regularDebitContractMaintenanceStart(this.detailForm)
            .then((response) => {
              this.msgSuccess("开启成功");
              this.open = false;
              this.loading = false;
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },

    //点击停用按钮
    pause() {
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          regularDebitContractMaintenancePause(this.detailForm)
            .then((response) => {
              this.msgSuccess("停用成功");
              this.open = false;
              this.loading = false;
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },

    //点击注销按钮
    cancellation() {
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          regularDebitContractMaintenanceCancel(this.detailForm)
            .then((response) => {
              this.msgSuccess("注销成功");
              this.open = false;
              this.loading = false;
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>
